package com.atguigu.tingshu;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.CategoryFeignClient;
import com.atguigu.tingshu.album.client.impl.AlbumInfoDegradeFeignClient;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.model.search.AlbumInfoIndex;
import com.atguigu.tingshu.model.search.AttributeValueIndex;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.album.AlbumListVo;
import com.atguigu.tingshu.vo.search.AlbumInfoIndexVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.bytebuddy.implementation.bytecode.Throw;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;

import java.util.List;

@SpringBootTest
public class UploadAlbumServiceSearch {

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;
    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;
    @Autowired
    private UserInfoFeignClient userInfoFeignClient;
    @Autowired
    private CategoryFeignClient categoryFeignClient;

    @Test
    public void initIndex() {
        // 1. 创建索引 和 映射
        IndexOperations ops = elasticsearchTemplate.indexOps(AlbumInfoIndex.class);
        if (!ops.exists()) {
            ops.create();
            ops.putMapping();
        }
    }

    @Test
    public void uploadDocument() {
        //  查询数据库数据 返回: AlbumInfoIndexVo对象
        // 1. 分页查询album_info 表 调用album微服务  // 注意专辑id的字段属性不同
        // 将每个专辑列表数据copy到AlbumInfoIndex
        // 遍历查询到的结果 得到albumId 和 userId
        // 2. 得到albumId 后查询sta统计信息计算热度
        // 3. 得到albumId后查询attr专辑属性和属性值id
        // 4. 根据albumId获取userId,根据userId查询用户信息
    }

    @Test
    public void testDocument() {
        // 测试远程调用接口是否可用
        Long pageNum = 1L;
        Long pageSize = 100L;
        Page<AlbumListVo> page = null;

        do {
            Result<Page<AlbumListVo>> result = albumInfoFeignClient.getAlbumInfoIndexPage(pageNum, pageSize);

            if (result == null || result.getData() == null) {
                throw new RuntimeException("远程调用失败");
            }
            page = result.getData();
            List<AlbumListVo> abumInfoIndexVoList = page.getRecords();
//          将 List<AlbumInfoIndexVo> 转换为 List<AlbumInfoIndex>
            List<AlbumInfoIndex> albumInfoIndexList = abumInfoIndexVoList.stream().map(
                    albumInfoVo -> {
                        AlbumInfoIndex albumInfoIndex = new AlbumInfoIndex();
                        BeanUtils.copyProperties(albumInfoVo, albumInfoIndex);
                        albumInfoIndex.setId(albumInfoVo.getAlbumId());
                        // 远程调用user微服务 获取用户信息
                        // 获取用户id
                        Long userId = albumInfoVo.getUserId();
                        // 根据用户id查询用户信息 远程调用 user
                        Result<UserInfoVo> userInfoVoResult = userInfoFeignClient.getUserInfoById(userId);
//                        if (userInfoVoResult == null || userInfoVoResult.getData() == null) {
//                            throw new RuntimeException("远程调用失败");
//                        }
                        // 将用户信息设置到AlbumInfoIndex
                        albumInfoIndex.setAnnouncerName(userInfoVoResult.getData().getNickname());
                        // 使用专辑id查询专辑属性列表
                        Result<List<AttributeValueIndex>> attributes = albumInfoFeignClient.findAttributesByAlbumId(Long.valueOf(albumInfoVo.getAlbumId()));
//                        if (attributes == null || attributes.getData() == null) {
//                            throw new RuntimeException("远程调用失败");
//                        }
                        List<AttributeValueIndex> attributeValueIndexList = attributes.getData();
                        albumInfoIndex.setAttributeValueIndexList(attributeValueIndexList);
                        // 使用专辑的三级分类id查询分类视图获取一二级分类id
                        Long category3Id = albumInfoVo.getCategory3Id();
                        Result<BaseCategoryView> baseCategoryViewResult = categoryFeignClient.getBaseCategoryViewByCategory3Id(category3Id);
//                        if (baseCategoryViewResult == null || baseCategoryViewResult.getData() == null) {
//                            throw new RuntimeException("远程调用失败");
//                        }
                        BaseCategoryView baseCategoryView = baseCategoryViewResult.getData();
                        albumInfoIndex.setCategory1Id(baseCategoryView.getCategory1Id());
                        albumInfoIndex.setCategory2Id(baseCategoryView.getCategory2Id());
                        // 计算根据专辑统计信息计算专辑热度
                        albumInfoIndex.setHotScore(albumInfoIndex.getSubscribeStatNum() * 0.3 + albumInfoIndex.getPlayStatNum() * 0.1 + albumInfoIndex.getCommentStatNum() * 0.2 + albumInfoIndex.getBuyStatNum() * 0.4);
                        return albumInfoIndex;
                    }
            ).toList();
            elasticsearchTemplate.save(albumInfoIndexList);
            pageNum++;
        } while (page.hasNext());

    }


    // 远程调用  根据用户id查询用户信息
    @Test
    public void test1() {

        // 远程调用  根据用户id查询用户信息
        Result<UserInfoVo> res1 = userInfoFeignClient.getUserInfoById(1L);
        Result<UserInfoVo> res2 = userInfoFeignClient.getUserInfoById(12L);


        UserInfoVo user1 = res1.getData();
        UserInfoVo user2 = res2.getData();

        System.out.println("user1 = " + user1);
        System.out.println("user2 = " + user2);
    }

    // 远程调用 根据专辑id查询专辑属性列表
    @Test
    public void test2() {
        // 远程调用 根据专辑id查询专辑属性列表
        for (int i = 1; i <= 100; i++) {
            Result<List<AttributeValueIndex>> attributes = albumInfoFeignClient.findAttributesByAlbumId(Long.valueOf(i));
            if (attributes == null || attributes.getData() == null) {
                throw new RuntimeException("远程调用失败");
            }
            List<AttributeValueIndex> attributeValueIndexList = attributes.getData();
            attributeValueIndexList.forEach(System.out::println);
        }
    }

    // 根据三级分类id获取分类视图对象 getBaseCategoryViewByCategory3Id
    @Test
    public void test3() {
        for (int i = 1; i <= 100; i++) {
            Result<BaseCategoryView> baseCategoryViewResult = categoryFeignClient.getBaseCategoryViewByCategory3Id(Long.valueOf(i + 1000));
            if (baseCategoryViewResult == null || baseCategoryViewResult.getData() == null) {
                throw new RuntimeException("远程调用失败");
            }
            BaseCategoryView baseCategoryView = baseCategoryViewResult.getData();
            System.out.println("baseCategoryView = " + baseCategoryView);
        }
        //endregion
    }

    // 初始化专辑的统计信息 比如订阅量、播放量、评论量、购买量
    @Test
    public void test4() {
            // 测试es的增删改查
        IndexOperations ops = elasticsearchTemplate.indexOps(AlbumInfoIndex.class);
    }
    // 根据专辑id获取专辑的信息
    @Test
    public void test05() {
        Result<AlbumListVo> albumInfoById = albumInfoFeignClient.getAlbumInfoById(1611L);
        System.out.println("albumInfoById = " + albumInfoById);
    }

}
